set more off
clear
cd "D:\Projects\HeterogeneousExternalities\Data\"

use GMaps.dta,clear


gen beta_log=.
gen beta_level=.
gen se_log=.
gen se_level=.
destring hour, gen(hour_byte)


levelsof county_fips, local(counties)
foreach county in `counties'{

	reghdfe lspeed lveh if county_fips=="`county'" , vce(r) absorb(OD)
	cap replace beta_log=_b[lveh] if county_fips=="`county'"
	cap replace se_log=_se[lveh] if county_fips=="`county'"

}


collapse (mean) speed beta_log se_log (first) urban , by(county_fips)


save SpeedDist.dta,replace

sum beta_log if urban==1,
local large : di %6.3f scalar(`r(mean)')
sum beta_log if urban==2,
local fringe : di %6.3f scalar(`r(mean)')
sum beta_log if urban==3,
local medium : di %6.3f scalar(`r(mean)')
sum beta_log if urban==4,
local small : di %6.3f scalar(`r(mean)')


twoway kdensity beta_log if urban==1 &  beta_log>-.08 , lwidth(medthick) || kdensity beta_log if urban==2,lwidth(medthick) || /*
	*/kdensity beta_log if urban==3,lwidth(medthick) || kdensity beta_log if urban==4,  lpattern(-..*.-) lwidth(thick)/*
	*/xtitle(Point Estimates) ytitle(Density) legend(label(1 "Large Central Metro (`large')")/*
	*/label(2 "Large Fringe Metro (`fringe')") label(3 "Medium Metro (`medium')") label(4 "Small Metro (`small')") pos(11) ) /*
	*/ legend(size(medium))  xlabel(,nogrid) ylabel(,nogrid) name(beta_log, replace) xline(0, lstyle(p6))


gen t_stat=beta_log/se_log
sum t_stat if urban==1,
local large : di %6.2f scalar(`r(mean)')
sum t_stat if urban==2,
local fringe : di %6.2f scalar(`r(mean)')
sum t_stat if urban==3,
local medium : di %6.2f scalar(`r(mean)')
sum t_stat if urban==4,
local small : di %6.2f scalar(`r(mean)')

	
twoway kdensity t_stat if urban==1,lwidth(medthick) || kdensity t_stat if urban==2,lwidth(medthick) || /*
	*/kdensity t_stat if urban==3,lwidth(medthick) || kdensity t_stat if urban==4 & t_stat<100,  lpattern(-.*..-) lwidth(thick)/*
	*/xtitle(T-Statistics) ytitle(Density) legend(label(1 "Large Central Metro (`large')")/*
	*/label(2 "Large Fringe Metro (`fringe')") label(3 "Medium Metro (`medium')") label(4 "Small Metro (`small')") pos(11) )  legend(size(medium)) /*
	*/ xlabel(,nogrid) ylabel(,nogrid) name(tstat,replace) xline(0, lstyle(p6))


local m=0
graph combine beta_log tstat ,  altshrink graphregion(color(white) margin(l=`m' r=`m')) col(2) 

	
	
	